package felix.things.router.mqtt.persistent;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.*;

import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

/**
 * @author Felix
 * @date 2019/7/27
 */
@Data
@Builder
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class MqttPersistentMessage implements Serializable {

    private static final long serialVersionUID = -3133461476074777891L;

    private UUID id;
    private long timestamp;
    private String topic;
    @JsonSerialize(using = ByteStringSerializer.class)
    private byte[] payload;

    @JsonCreator
    public MqttPersistentMessage(@JsonProperty("id") UUID id, @JsonProperty("timestamp") long timestamp,
                                 @JsonProperty("topic") String topic, @JsonProperty("payload") String payload) {
        this.id = id;
        this.timestamp = timestamp;
        this.topic = topic;
        this.payload = payload.getBytes(StandardCharsets.UTF_8);
    }
}
